package com.yiwei.zxp.adapter;

import android.content.Context;
import android.graphics.Typeface;
import android.support.v7.widget.RecyclerView;
import android.text.Spanned;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.yiwei.zxp.R;
import com.yiwei.zxp.api.model.PayWayListModel;
import com.yiwei.zxp.constant.Const;
import com.yiwei.zxp.constant.UserInfoGlobal;
import com.yiwei.zxp.util.Spans;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * Created by xsooy-pc on 2017/10/23.
 */

public class PacketPayTypeAdapter extends ItemAdapter<PayWayListModel.DatasBean, PacketPayTypeAdapter.DefaultHolder> {

    private Context context;
    private final int x24;
    private final int x26;
    private String xianXia;

    public PacketPayTypeAdapter(Context context) {
        super(context);
        this.context = context;
        x26 = context.getResources().getDimensionPixelSize(R.dimen.x26);
        x24 = context.getResources().getDimensionPixelSize(R.dimen.x24);
    }

    @Override
    public DefaultHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new DefaultHolder(inflate(R.layout.item_packet_pay_type, parent));
    }

    @Override
    public void onBindViewHolder(DefaultHolder holder, int position) {
        PayWayListModel.DatasBean item = getItem(position);
        if (item.en_name.equals("union")) {
            Spanned txt_info = Spans.with(context).font(item.name, x26, 0xff000000).style(Typeface.BOLD).
                    font("（单笔限额：" + UserInfoGlobal.getBankDayLimit() + "元）", x24, 0xff999999).
                    build();
            holder.txtPayName.setText(txt_info);
        } else {
            holder.txtPayName.setText(item.name);
            if (item.en_name.equals(Const.PayWay.ULINE)) {
                xianXia = item.id;
            }
        }

        if (item.isCheck) {
            holder.btnCheck.setImageResource(R.mipmap.img_service_select);
        } else {
            holder.btnCheck.setImageResource(R.mipmap.img_service_unselect);
        }
        holder.btnCheck.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (int i = 0; i < getItemCount(); i++) {
                    if (i == position) {
                        getmSource().get(i).isCheck = true;
                    } else {
                        getmSource().get(i).isCheck = false;
                    }
                    notifyDataSetChanged();
                }
                if (onChooseListener != null) {
                    onChooseListener.onClick(item.id, item.en_name, xianXia);
                }
            }
        });
    }

    class DefaultHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.btn_check)
        ImageView btnCheck;
        @BindView(R.id.txt_pay_name)
        TextView txtPayName;

        public DefaultHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    private OnChooseListener onChooseListener;

    public void setOnChooseListener(OnChooseListener onChooseListener) {
        this.onChooseListener = onChooseListener;
    }

    public interface OnChooseListener {
        void onClick(String id, String en_name, String xianXia);
    }
}
